$(function () {
    obj.init();
});

var obj = new function () {
    'use strict';
    return {
        form: null,
        targetObj: null,
        dom: {
            typeDom: null,
            saveDom: null
        },
        params: {
            code: null,
            type: null,
            userName: null,
            phoneNum: null,
            schoolId: null,
            userId:   null,
            usePerson:null
        },
        init: function () {
            this.form = $('#form');

            this.dom.typeDom = $('#type');
            this.dom.saveDom = $('#save');

            this.listener();
        },
        listener: function () {
            var $this = this;
            //类型选中事件
            $this.dom.typeDom.combobox({
                onSelect: function (rec) {
                    $this.params.type = rec['value'];
                    $this.funObj().tiggerTypeChange();
                }
            });

            //学校
            $this.form.on('click',('.textbox-button'),function () {  //textBox按钮点击事件
                // window.webkit.messageHandlers.openScanf.postMessage(null);
                window.top.openScan();
            });

            $this.form.on('click','#save',function () {
                var code = $('#code').textbox('getValue');
                if (code == '') {
                    window.top.sendErrorMsg('设备码不能为空');
                    return false;
                }
                var pam= /^\d{1,10}$/;
                var $res = pam.test(code);
                if($res){
                    var todoFlag = false;
                    window.top.sendWarn('设备码：'+code+'为1-10位纯数字,你确定要提交么',function () {
                        save(code);
                    },function () {
                        return false;
                    });
                }else {
                    save(code);
                }
            });
        },
        funObj: function () {
            var $this = this;
            return {
                tiggerTypeChange: function () {
                    if ($this.targetObj != null) {
                        $this.targetObj.remove();
                    }
                    var str = '';
                    if ($this.params.type == 1 || $this.params.type == 2) {//自用机/备用机
                        str += '<div style="margin-bottom:10px">'
                            + '<input id="phoneNum" class="easyui-textbox" label="手机:" name="phoneNum" data-options="prompt:\'电话：\',labelPosition:\'top\'" style="width:100%">'
                            + '</div>';

                        str += '<div style="margin-bottom:10px">'
                            + '<input id="userName" class="easyui-textbox" label="姓名:" name="userName" data-options="prompt:\'姓名：\',labelPosition:\'top\'" style="width:100%">'
                            + '</div>';
                        if($this.params.type == 1){
                            str += '<div style="margin-bottom:10px">' +
                                '<select id="bDemo" class="easyui-combobox" label="演示机:" name="bDemo" style="width:100%" data-options="editable:true,labelPosition:\'top\'">'+
                                    '<option value="1" >是</option>'+
                                    '<option value="0" selected>否</option>'+
                                '</select>' +
                                '</div>';

                        }

                        if($this.params.type == 2){
                            str += '<div style="margin-bottom:10px">' +
                                    '<select id="schoolId" class="easyui-combobox" label="学校:" name="schoolId" style="width:100%" data-options="editable:true,labelPosition:\'top\'"></select>' +
                                '</div>';
                        }
                    } else if ($this.params.type == 3) { //教师机
                        str += '<div style="margin-bottom:10px">' +
                                    '<select id="schoolId" class="easyui-combobox" label="学校:" name="schoolId" style="width:100%" data-options="editable:true,labelPosition:\'top\'"></select>' +
                                '</div>';
                        str += '<div style="margin-bottom:10px">' +
                                    '<select id="userId" class="easyui-combobox" label="老师:" name="userId" style="width:100%" data-options="editable:true,labelPosition:\'top\'"></select>' +
                                '</div>';
                        // str += '<div style="margin-bottom:10px">'
                        //     + '<input id="usePerson" class="easyui-textbox" label="使用者:" name="usePerson" data-options="prompt:\'使用者：\',labelPosition:\'top\'" style="width:100%">'
                        //     + '</div>';
                    } else if ($this.params.type == 4) { //公用机
                        str += '<div style="margin-bottom:10px">' +
                            '<select id="schoolId" class="easyui-combobox" label="学校:" name="schoolId" style="width:100%" data-options="editable:true,labelPosition:\'top\'"></select>' +
                            '</div>';
                    }
                    str += '<div style="margin-bottom:10px">'
                            +'<input id="code" class="easyui-textbox" label="设备码:"  name="code" data-options="buttonText:\'扫描\',buttonIcon:\'btn-scan\',prompt:\'设备码\',labelPosition:\'top\'" style="width:100%">'
                        +'</div>';
                    str += '<div style="margin-bottom:10px">' +
                        '        <a id="save" class="easyui-linkbutton" data-options="iconCls:\'btn-save\'" style="padding:5px 0px;width:100px;float: right;margin-right: 5px;">' +
                        '            <span style="font-size:14px;">提交</span>' +
                        '        </a>' +
                        '    </div>';

                    $this.targetObj = $(str);
                    $this.form.append($this.targetObj);
                    $.parser.parse($this.targetObj);
                    if($this.params.type == 1 || $this.params.type == 2){
                        $this.funObj().phoneEvent();
                    }
                    $this.funObj().initSchool();//初始化学校
                },
                initSchool:function () {
                    if($this.params.type == 2 || $this.params.type == 3 ||$this.params.type == 4){
                        var url = baseUrl+'mobile/shortcut/getSchTch';
                        $('#schoolId').combobox({
                            url:url,
                            valueField:'iSchoolId',
                            textField:'sSchoolName',
                            onSelect: function (rec) {
                                $this.params.schoolId = rec['iSchoolId'];
                                if($this.params.type == 3){
                                    $this.funObj().tiggerSchoolChange();
                                }

                            },
                            filter: function(q, row){
                                var opts = $(this).combobox('options');
                                return row[opts.textField].indexOf(q) >= 0;
                            }
                        });
                    }
                },
                tiggerSchoolChange:function () {
                    $('#userId').combobox({
                        url:baseUrl+'mobile/shortcut/getSchTch?schoolId='+$this.params.schoolId,
                        valueField:'iUserId',
                        textField:'sUserName',
                        onSelect: function (rec) {
                            $this.params.userId = rec['iUserId'];
                        }
                    })
                },
                phoneEvent:function () {
                    $('#phoneNum').textbox('textbox').bind('keyup', function(e){
                        var  val = $(this).val();
                        var phoneReg =  /^1[0-9]{10}$/;
                        if(phoneReg.test(val)){
                            if($this.params.type == 2){  //触发学校
                                $this.funObj().schoolEvent(val);
                            }
                            $.ajax({
                                url:baseUrl+'mobile/shortcut/getNameByPhone',
                                data:{phone:val},
                                type:'post',
                                dataType:'json',
                                success:function (data) {
                                    $('#userName').textbox('setValue',data['sStaffName']);
                                },
                                error:function (data) {
                                    $('#userName').textbox('setValue','');
                                    window.top.sendErrorMsg(data['responseText']);
                                }
                            })

                        }else{
                            $('#userName').textbox('setValue','');
                        }
                    });
                },
                schoolEvent:function (phone) {
                    var url = baseUrl+'mobile/shortcut/getSchTch';
                    if(phone!= null ){
                        url += '/'+phone;
                    }
                    $('#schoolId').combobox({
                        url:url,
                        valueField:'iSchoolId',
                        textField:'sSchoolName',
                        onSelect: function (rec) {
                            $this.params.schoolId = rec['iSchoolId'];
                            if($this.params.type == 3){
                                $this.funObj().tiggerSchoolChange();
                            }

                        },
                        filter: function(q, row){
                            var opts = $(this).combobox('options');
                            return row[opts.textField].indexOf(q) >= 0;
                        }
                    });
                }
            }
        }
    }
};




/**
 * 扫码回调
 * @type {{setResult: JSObjectHandle.setResult}}
 * created by dale 2017/9/15
 */
var JSObjectHandle = {
    setResult: function (code) {
        setTimeout(function () {
            $('#code').textbox('setValue',code);


        }, 0);

    }
};

function save(code) {
    // $('#code').textbox('setValue',code);
    $.ajax({
        url:baseUrl+'mobile/shortcut/receiveNewTablet',
        data:obj.form.serialize(),
        type:'post',
        success:function (data) {
            window.top.swal({title:'成功',text: data,type:'success',confirmButtonText:'确定'}).then(function () {
                $('#code').textbox('setValue','');
            },function () {
                return false
            });

        },
        error:function (data) {
            window.top.sendErrorMsg(data['responseText']);
        }
    });
}